package org.zfes.snowier.zbss.xzy.biz.config.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZObjectUtil;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;

import com.google.common.collect.Lists;

import org.zfes.snowier.zbss.xzy.biz.config.dao.ConfigItemValueMapper;
import org.zfes.snowier.zbss.xzy.biz.config.model.ConfigItem;
import org.zfes.snowier.zbss.xzy.biz.config.model.ConfigItemValue;
import org.zfes.snowier.zbss.xzy.biz.config.model.ConfigRecord;
import org.zfes.snowier.zbss.xzy.biz.config.service.IConfigItemService;
import org.zfes.snowier.zbss.xzy.biz.config.service.IConfigItemValueService;
import org.zfes.snowier.zbss.xzy.biz.config.service.IConfigRecordService;
import org.zfes.snowier.zbss.xzy.biz.dto.AIConfigItemValueDto;

import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class ConfigItemValueServiceImpl implements IConfigItemValueService{
 	@Autowired
	private ConfigItemValueMapper aIConfigItemValueMapper;
	@Autowired
	private IConfigItemService aIConfigItemService;
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createAIConfigItemValue(ConfigItemValue aIConfigItemValue) {
		ZBeanUtil.setCreateInfo(aIConfigItemValue);
		ZBeanUtil.validateBean(aIConfigItemValue);
		aIConfigItemValueMapper.insertSelective(aIConfigItemValue);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ConfigItemValue> loadAIConfigItemValueById(Long id) {
		if(id!=null){
			return Optional.ofNullable(aIConfigItemValueMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateAIConfigItemValue(ConfigItemValue aIConfigItemValue) {
		ZBeanUtil.setUpdatedInfo(aIConfigItemValue);
		ZBeanUtil.validateBean(aIConfigItemValue);
		aIConfigItemValueMapper.updateByPrimaryKeySelective(aIConfigItemValue);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteAIConfigItemValue(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		aIConfigItemValueMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setAIConfigItemValueStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		aIConfigItemValueMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadAIConfigItemValueDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(aIConfigItemValueMapper.selectListCount(pm), aIConfigItemValueMapper.selectMapListPage(pm));
		return ds;
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<AIConfigItemValueDto> loadAIConfigItemValue(Long cateId, Long deviceId){
		List<ConfigItemValue> itemValues= aIConfigItemValueMapper.selectByCateAndDeviceId(cateId,deviceId);
		if(itemValues==null||itemValues.isEmpty()) {
			//return Lists.newArrayList();
		}
		List<ConfigItem> itemList = aIConfigItemService.loadItemByCate(cateId, Byte.valueOf("1"));
		if(ZObjectUtil.isEmpty(itemList)) {
			return Lists.newArrayList();
		}
		
		
			
		List<AIConfigItemValueDto> resList = itemList.stream().map(item->{
				
				ConfigItemValue itemVlaue= itemValues.stream().filter(iv->iv.getItemId().equals(item.getId())).findAny().orElse(null);
				
				AIConfigItemValueDto itemValueDto = new AIConfigItemValueDto();
				itemValueDto.setCateId(cateId)
						.setDeviceId(deviceId)
						.setItemCode(item.getCode())
						.setItemId(item.getId())
						.setItemName(item.getName())
						.setRemark(item.getRemark())
						.setCofvalue(itemVlaue==null?"":(ZStrUtil.hasText(itemVlaue.getCofvalue())?itemVlaue.getCofvalue():""));
				
				return itemValueDto;
				
			}).collect(Collectors.toList());
			
			
		return resList;
		
	}
	@Autowired IConfigRecordService configRecordService;
	@Transactional(rollbackFor=Exception.class,readOnly=false)
	@Override
	public void saveItemValues(Long cateId, Long deviceId,List<AIConfigItemValueDto> itemValues) {
		
		aIConfigItemValueMapper.deleteByCateAndDeviceId(cateId,deviceId);
		
		List<ConfigItem> itemList = aIConfigItemService.loadItemByCate(cateId, Byte.valueOf("1"));
		
		List<ConfigItemValue> resList = itemList.stream().map(item->{
			
			AIConfigItemValueDto value=itemValues.stream()
					.filter(v->cateId.equals(v.getCateId())&&deviceId.equals(v.getDeviceId())&&v.getItemId().equals(item.getId()))
					.findAny().orElse(null);
			
			String confValue=value==null?"":value.getCofvalue();
			
			return  new ConfigItemValue().setCateId(cateId).setDeviceId(deviceId).setItemId(item.getId()).setCofvalue(confValue);
			
		}).collect(Collectors.toList());
		
		resList.forEach(vl->{
			aIConfigItemValueMapper.insertSelective(vl);
		});

		//触发--tcp通知，
		configRecordService.createConfigRecord(new ConfigRecord( cateId,  deviceId, Byte.valueOf("0"),  0) );
		
		
		
	}
}